"""
插入行对话框 - 允许用户在指定位置插入行
"""

from PySide6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QWidget,
                               QPushButton, QLabel, QSpinBox,
                               QFormLayout, QDialogButtonBox, QGroupBox,
                               QRadioButton, QButtonGroup, QComboBox)
from PySide6.QtCore import Qt
from typing import Optional, Tuple


class InsertRowDialog(QDialog):
    """插入行对话框"""
    
    def __init__(self, current_row: int, total_rows: int, parent=None):
        super().__init__(parent)
        self.current_row = current_row
        self.total_rows = total_rows
        
        self.setWindowTitle("插入行")
        self.setModal(True)
        self.setFixedSize(400, 300)
        
        self._setup_ui()
        
    def _setup_ui(self):
        """设置UI"""
        layout = QVBoxLayout(self)
        
        # 行数设置
        row_count_group = QGroupBox("插入行数")
        row_count_layout = QVBoxLayout(row_count_group)
        
        self.row_count_spinbox = QSpinBox()
        self.row_count_spinbox.setMinimum(1)
        self.row_count_spinbox.setMaximum(1000)
        self.row_count_spinbox.setValue(1)
        self.row_count_spinbox.setSuffix(" 行")
        
        row_count_layout.addWidget(self.row_count_spinbox)
        layout.addWidget(row_count_group)
        
        # 插入位置选择
        position_group = QGroupBox("插入位置")
        position_layout = QVBoxLayout(position_group)
        
        self.position_group = QButtonGroup()
        
        # 在当前行之前插入
        self.before_radio = QRadioButton(f"在当前行之前 (第 {self.current_row + 1} 行)")
        self.position_group.addButton(self.before_radio)
        position_layout.addWidget(self.before_radio)
        
        # 在当前行之后插入
        self.after_radio = QRadioButton(f"在当前行之后 (第 {self.current_row + 2} 行)")
        self.position_group.addButton(self.after_radio)
        position_layout.addWidget(self.after_radio)
        
        # 在末尾添加
        self.end_radio = QRadioButton("在末尾添加")
        self.position_group.addButton(self.end_radio)
        position_layout.addWidget(self.end_radio)
        
        # 默认选择
        if self.current_row < self.total_rows:
            self.after_radio.setChecked(True)
        else:
            self.end_radio.setChecked(True)
            
        layout.addWidget(position_group)
        
        # 填充模式
        fill_group = QGroupBox("填充模式")
        fill_layout = QVBoxLayout(fill_group)
        
        self.fill_combo = QComboBox()
        self.fill_combo.addItems([
            "空白值",
            "复制上一行",
            "复制当前行",
            "智能默认值"
        ])
        self.fill_combo.setCurrentIndex(0)  # 默认空白值
        fill_layout.addWidget(self.fill_combo)
        
        layout.addWidget(fill_group)
        
        # 说明文本
        info_label = QLabel("提示：可以使用快捷键 Ctrl+Shift+N 快速插入行")
        info_label.setStyleSheet("color: gray; font-size: 10px;")
        layout.addWidget(info_label)
        
        # 按钮
        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)
        
    def get_row_info(self) -> Optional[Tuple[int, int, int]]:
        """
        获取行信息：行数、插入位置、填充模式
        返回: (count, position, fill_mode_index)
        """
        if self.result() == QDialog.Accepted:
            count = self.row_count_spinbox.value()
            
            # 确定插入位置
            if self.before_radio.isChecked():
                position = self.current_row
            elif self.after_radio.isChecked():
                position = self.current_row + 1
            else:
                position = self.total_rows
                
            fill_mode_index = self.fill_combo.currentIndex()
            
            return count, position, fill_mode_index
        
        return None
